package com.woniuxy.processservice.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.core.model.Result;
import com.woniuxy.core.util.DateUtil;
import com.woniuxy.core.util.PageInfo;
import com.woniuxy.processclient.client.CardFeignClient;
import com.woniuxy.processclient.client.OrderFeignClient;
import com.woniuxy.processclient.client.dto.CountCardDto;
import com.woniuxy.processclient.client.dto.OrderDto;
import com.woniuxy.processdao.mapper.CountCardStepMapper;
import com.woniuxy.processdao.model.CountCardStep;
import com.woniuxy.processmodel.dto.CountCardStepDto;
import com.woniuxy.processmodel.form.UpdateCountStepForm;
import com.woniuxy.processmodel.form.VenueLookCountCardStepForm;
import com.woniuxy.processservice.exception.ProcessException;
import com.woniuxy.processservice.service.CountCardStepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 次卡流程表 服务实现类
 * </p>
 *
 * @author 冉杰
 * @since 2024-01-15
 */
@Service
public class CountCardStepServiceImpl extends ServiceImpl<CountCardStepMapper, CountCardStep> implements CountCardStepService {

    @Autowired
    private CountCardStepMapper countCardStepMapper;
    @Resource
    private OrderFeignClient orderFeignClient;
    @Resource
    private CardFeignClient cardFeignClient;
    @Override
    public void addCountCardStep(String orderNum) {
        QueryWrapper<CountCardStep> wrapper = new QueryWrapper<>();
        wrapper.eq("order_num",orderNum);
        CountCardStep step = countCardStepMapper.selectOne(wrapper);
        if(!BeanUtil.isEmpty(step)){
            throw new ProcessException(4001,"该订单次卡流程已添加");
        }
        OrderDto data = orderFeignClient.findByOrderNum(orderNum).getData();
        if(BeanUtil.isEmpty(data)){
            throw new ProcessException(4009,"该订单不存在");
        }
        CountCardDto cardDto = cardFeignClient.findById(data.getProductId()).getData();
        CountCardStep countCardStep = new CountCardStep();
        countCardStep.setOrderNum(orderNum);
        countCardStep.setVenueId(data.getVenueId());
        countCardStep.setCountCardId(data.getProductId());
        countCardStep.setCountCardName(data.getProductName());
        countCardStep.setUserId(data.getUserId());
        countCardStep.setStartTime(data.getOrderCreateTime());
        countCardStep.setResidueCount(cardDto.getCardCount());
        countCardStepMapper.insert(countCardStep);
    }

    @Override
    public void updateCountCardStep(UpdateCountStepForm form, Integer venueId) {
        QueryWrapper<CountCardStep> wrapper = new QueryWrapper<>();
        wrapper.eq("order_num",form.getOrderNum());
        CountCardStep step = countCardStepMapper.selectOne(wrapper);
        if(BeanUtil.isEmpty(step)){
            throw new ProcessException(4002,"该订单次卡流程不存在");
        }
        if(step.getVenueId()!=venueId){
            throw new ProcessException(4003,"场馆信息有误");
        }
        if(step.getResidueCount()==0){
            throw new ProcessException(4004,"该订单次卡进度已为0");
        }
        Result<OrderDto> result = orderFeignClient.findByOrderNum(step.getOrderNum());
        OrderDto data = result.getData();
        if(data.getOrderState()!=1){
            throw new ProcessException(4005,"该订单次卡状态异常");
        }
        step.setResidueCount(step.getResidueCount()-1);
        step.setCompleteTime(DateUtil.dateTimeToString(new Date()));
        countCardStepMapper.updateById(step);
        //修改次卡订单状态
        orderFeignClient.updateOrderStateByOrderNum(form.getOrderNum(),3);
    }

    @Override
    public CountCardStep lookCountCardStep(Integer stepId) {
        CountCardStep countCardStep = countCardStepMapper.selectById(stepId);
        return countCardStep;
    }

    @Override
    public List<CountCardStepDto> UserLookCountCardStep(Integer userId) {
        QueryWrapper<CountCardStep> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<CountCardStep> countCardSteps = countCardStepMapper.selectList(wrapper);
        List<CountCardStepDto> dtos = BeanUtil.copyToList(countCardSteps, CountCardStepDto.class);
        return dtos;
    }

    @Override
    public PageInfo<CountCardStepDto> venueLookCountCardStep(VenueLookCountCardStepForm form, Integer venueId) {
        QueryWrapper<CountCardStep> wrapper = new QueryWrapper<>();
        wrapper.like(!BeanUtil.isEmpty(form.getOrderNum()),"order_num",form.getOrderNum());
        wrapper.eq(!BeanUtil.isEmpty(form.getCountCardName()),"count_card_name",form.getCountCardName());
        wrapper.eq("venue_id",venueId);
        Page<CountCardStep> page = new Page<>(form.getPageNum(), form.getPageSize());
        page(page,wrapper);
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        pageInfo.setList(BeanUtil.copyToList(page.getRecords(),CountCardStepDto.class));
        return pageInfo;
    }
}
